import React, { useState, useEffect } from 'react'
import { Col, Form, Input, Button, Table, Select, DatePicker, Row } from 'antd'
import { PageHeaderWrapper } from '@ant-design/pro-layout'
import RenderButtom from '@/components/RenderButton'
import SearchBox from '@/components/SearchBox'
import { getHandleRecord } from '@/services/userCenter/card/handle'

const { Option } = Select

const { RangePicker } = DatePicker

// 表头
const columns = [
  {
    title: '会员卡号',
    dataIndex: 'userCardNo',
    key: 'userCardNo',
    align: 'center'
  },
  {
    title: '会员卡名称',
    dataIndex: 'cardName',
    key: 'cardName',
    align: 'center'
  },
  {
    title: '用户昵称',
    dataIndex: 'userName',
    key: 'userName',
    align: 'center'
  },
  {
    title: '订单号',
    dataIndex: 'orderNo',
    key: 'orderNo',
    align: 'center'
  },
  {
    title: '操作金额（元）',
    dataIndex: 'handleMoney',
    key: 'handleMoney',
    align: 'center'
  },
  {
    title: '操作前余额（元）',
    dataIndex: 'beforeHandleMoney',
    key: 'beforeHandleMoney',
    align: 'center'
  },
  {
    title: '操作后余额（元）',
    dataIndex: 'afterHandleMoney',
    key: 'afterHandleMoney',
    align: 'center'
  },
  {
    title: '操作赠送金（元）',
    dataIndex: 'giveMoney',
    key: 'giveMoney',
    align: 'center'
  },
  {
    title: '操作类型',
    dataIndex: 'type',
    key: 'type',
    align: 'center'
  },
  {
    title: '状态',
    dataIndex: 'status',
    key: 'status',
    align: 'center'
  }
  ,{
    title: '交易成功时间',
    dataIndex: 'time',
    key: 'time',
    align: 'center'
  }
]

const Handle = props => {
  const { form } = props
  const { getFieldDecorator } = form
  const [handleRecord, setHandleRecord] = useState([])
  const [total, setTotal] = useState(1)
  const [pageNo, setPageNo] = useState(1)
  const [pageSize, setPageSize] = useState(10)
  const [record, setRecord] = useState({})
  const [startTime, setStartTime] = useState('')
  const [endTime, setEndTime] = useState('')
  const [typeList, setTypeList] = useState([
    {
      id: 1,
      name: '充值'
    },
    {
      id: 2,
      name: '消费'
    },
    {
      id: 3,
      name: '退款'
    },
    {
      id: 4,
      name: '积分变更'
    }
  ])
  const [statusList, setStatusList] = useState([
    {
      id: 0,
      name: '操作中'
    },
    {
      id: 1,
      name: '操作成功'
    },
    {
      id: 2,
      name: '操作失败'
    },
    {
      id: 3,
      name: '操作取消'
    }
  ])

  const rowSelection = {
    type: 'radio',
    onChange: (selectedRowKeys, selectedRows) => {
      setRecord(selectedRows)
      // console.log(`selectedRowKeys: ${selectedRowKeys}`, 'selectedRows: ', selectedRows);
      console.log(record)
    },
  }

  useEffect(() => {
    init()
  }, [])
  const init = async () => {
    let params = {
      pageNo,
      pageSize,
      startTime,
      endTime,
      status: '',
      type: '',
      userCardNo: '',
      orderNo: ''
    }
    return
    await getHandleRecords(params)
  }
  // 表单查询
  const handleSearch = e => {
    e.preventDefault()
    form.validateFields(async (err, values) => {
      console.log('Received values of form: ', values)
      let params = {
        pageNo,
        pageSize,
        startTime,
        endTime,
        status: values.status,
        type: values.type,
        userCardNo: values.userCardNo,
        orderNo: values.orderNo
      }
      console.log(params)
      return
      await getHandleRecords(params)
    })
  }
  // 表单清空
  const handleReset = e => {
    form.resetFields();
  }
  // 查询条件
  const getFields = () => {
    return (
      <Row gutter={24}>
        <Col span={12}>
          <Form.Item label="会员卡号" labelCol={{ span: 4 }} wrapperCol={{ span: 20 }} >
            {getFieldDecorator('userCardNo', {})
              (<Input placeholder="请输入会员卡号" />)}
          </Form.Item>
        </Col>
        <Col span={12}>
          <Form.Item label="订单号" labelCol={{ span: 4 }} wrapperCol={{ span: 20 }} >
            {getFieldDecorator('orderNo', {})
              (<Input placeholder="请输入订单号" />)}
          </Form.Item>
        </Col>
        <Col span={12}>
          <Form.Item label="操作类型" labelCol={{ span: 4 }} wrapperCol={{ span: 20 }} >
            {getFieldDecorator('type', {})
              (<Select
                style={{ width: '100%' }}
                showSearch
                filterOption={(input, option) => option.props.children.indexOf(input) >= 0}
                placeholder="请选择"
              >
                { typeList && typeList.map(r => <Option key={r.id} value={r.id}>{r.name}</Option>)}
              </Select>)}
          </Form.Item>
        </Col>
        <Col span={12}>
          <Form.Item label="操作状态" labelCol={{ span: 4 }} wrapperCol={{ span: 20 }} >
            {getFieldDecorator('status', {})
              (<Select
                style={{ width: '100%' }}
                showSearch
                filterOption={(input, option) => option.props.children.indexOf(input) >= 0}
                placeholder="请选择"
              >
                { statusList && statusList.map(r => <Option key={r.id} value={r.id}>{r.name}</Option>)}
              </Select>)}
          </Form.Item>
        </Col>
        <Col span={12}>
          <Form.Item label="时间" labelCol={{ span: 4 }} wrapperCol={{ span: 20 }} >
            {getFieldDecorator('time', {})
              (<RangePicker onChange={selectData} />)}
          </Form.Item>
        </Col>
      </Row>
    )
  }
  // 按钮处理
  const handleBtn = type => {

  }

  // 获取卡操作记录
  const getHandleRecords = async (params) => {
    let res = await getHandleRecord(params)
    if (res.code === '0') {
      setHandleRecord(res.data)
      setTotal(res.iTotalDisplayRecords)
    } else {
      alert(res.msg)
    }
  }

  const pagination = {
    total: parseInt(total),
    onChange(page) {
      let params = {
        pageNo: page,
        pageSize,
        startTime,
        endTime,
        status: '',
        type: '',
        userCardNo: '',
        orderNo: ''
      }
      getHandleRecords(params)
    }
  }
  // 选择时间
  const selectData = (date, dateString) => {
    setStartTime(dateString[0])
    setEndTime(dateString[1])
  }

  return (
    <PageHeaderWrapper>
      <SearchBox handleReset={handleReset} handleSearch={handleSearch} getFields={getFields} />
      <RenderButtom click={handleBtn} />
      <Table
        columns={columns}
        dataSource={handleRecord}
        rowKey={record => record.id}
        pagination={pagination}
        rowSelection={rowSelection}
      />
   </PageHeaderWrapper>
  )
}

export default Form.create()(Handle)
